Xceed DataGrid for WPF v7.2 Documentation
Creating a Value-changed Handler

Welcome to Xceed DataGrid, Editors, and 3D Views for WPF v7.2 > Xceed DataGrid for WPF > Code Snippets > Creating a Value-changed Handler

The following example demonstrates how to create a DependencyPropertyDescriptor for the ItemsSource property that will provide a value-changed handler so that we can be notified when the value of the ItemsSource property is changed.

The value of the ItemsSource property is changed to the Employees table when the button located above the grid is clicked.

XAML
Copy Code
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
  <Grid.Resources>
    <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                    Source="{Binding Source={x:Static Application.Current},
                                                      Path=Orders}"/>
  </Grid.Resources>
  <DockPanel>
    <Button Content="Change data source"
            DockPanel.Dock="Top"
            Click="ChangeDataSource"/>
      <xcdg:DataGridControl x:Name="OrdersGrid"
                            ItemsSource="{Binding Source={StaticResource cvs_orders}}"
                            DockPanel.Dock="Bottom"/>
  </DockPanel>
</Grid>

The DependencyPropertyDescriptor for the ItemsSource property is defined in the OnInitialized override and will call the OnDataGridItemsSourceChanged method when the ItemsSource property has been changed. The ChangeDataSource method will be called by the button to change the ItemsSource property.

VB.NET
Copy Code
Protected Overrides Sub OnInitialized( ByVal e As EventArgs )
  MyBase.OnInitialized( e ) 
  Dim gridItemsSourceDescriptor As DependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty( DataGridControl.ItemsSourceProperty, GetType( DataGridControl ) )
  gridItemsSourceDescriptor.AddValueChanged( Me.OrdersGrid, _
                                             New EventHandler( AddressOf OnDataGridItemsSourceChanged ) )
End Sub
Protected Sub OnDataGridItemsSourceChanged( ByVal sender As Object, _
                                            ByVal args As EventArgs )
  ' Before assigning the ItemsSource, UpdateLayout must be called to ensure that the
  ' DataGridControl's template is created and sized correctly in the page or window.
  Me.OrdersGrid.UpdateLayout()
  Dim column As Column
  For Each column In Me.OrdersGrid.Columns
    Dim width As Double = column.GetFittedWidth()
    If width > -1 Then
      column.Width = width
    End IF
  Next column
End Sub
Private Sub ChangeDataSource( ByVal sender As object, ByVal e As RoutedEventArgs )
  Me.OrdersGrid.Columns.Clear()
  Me.OrdersGrid.ItemsSource = Nothing
     
  Me.OrdersGrid.ItemsSource = New DataGridCollectionView( App.Employees.DefaultView )
End Sub
C#
Copy Code
protected override void OnInitialized( EventArgs e )
{
  base.OnInitialized( e );
 DependencyPropertyDescriptor gridItemsSourceDescriptor = DependencyPropertyDescriptor.FromProperty( DataGridControl.ItemsSourceProperty, typeof( DataGridControl ) );
 gridItemsSourceDescriptor.AddValueChanged( this.OrdersGrid, OnDataGridItemsSourceChanged );
}
protected void OnDataGridItemsSourceChanged( object sender, EventArgs args )
{
  // Before assigning the ItemsSource, UpdateLayout must be called to ensure that the
  // DataGridControl's template is created and sized correctly in the page or window.
  this.OrdersGrid.UpdateLayout();
  foreach( Column column in this.OrdersGrid.Columns )
  {
    double width = column.GetFittedWidth();
    if( width > -1 )
    {
      column.Width = width;
    }
  }
}
private void ChangeDataSource( object sender, RoutedEventArgs e )
{
  this.OrdersGrid.Columns.Clear();
  this.OrdersGrid.ItemsSource = null;
     
  this.OrdersGrid.ItemsSource = new DataGridCollectionView( App.Employees.DefaultView );
}